package util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @Author: ZeYu
 * @Date: 2022/3/14 14:25
 */
public class DruidUtils {
    //DruidDataSource  ×   DataSource√
    private static DataSource dataSource;

    static {
        InputStream inputStream = DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties");

        Properties properties = new Properties();
        //类加载器可以获取到流
        try {
            properties.load(inputStream);

            dataSource= DruidDataSourceFactory.createDataSource(properties);



        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static DataSource getDataSource(){
        return dataSource;
    }

    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }




}
